package com.irdstudio.rsp.rule.api.rest;

import com.irdstudio.rsp.rule.common.BizUtil;
import com.irdstudio.rsp.rule.service.facade.SRuleInfoService;
import com.irdstudio.rsp.rule.service.vo.SRuleCollDetailVO;
import com.irdstudio.rsp.rule.service.vo.SRuleInfoVO;
import com.irdstudio.rsp.rule.service.vo.SRuleInfoWithParamVO;
import com.irdstudio.sdk.beans.core.vo.ResponseData;
import com.irdstudio.sdk.beans.web.controller.AbstractController;
import com.irdstudio.sdk.ssm.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api")
public class SRuleInfoController extends AbstractController  {
	
	@Autowired
	@Qualifier("sRuleInfoServiceImpl")
	private SRuleInfoService sRuleInfoService;

	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/s/rule/infos", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SRuleInfoVO>> querySRuleInfoAll(
			SRuleInfoVO vo) {		
		List<SRuleInfoVO> outputVo = sRuleInfoService.queryAllOwner(vo);
		return getResponseData(outputVo);
		
	}

	/**
	 * 列表数据查询 排除指定规则集已关联规则
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/s/rule/infos/coll", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SRuleInfoVO>> queryAllExcludeRuleColl(
			SRuleCollDetailVO vo) {
		List<SRuleInfoVO> outputVo = sRuleInfoService.queryAllExcludeRuleColl(vo);
		return getResponseData(outputVo);

	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/s/rule/info/{ruleId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SRuleInfoVO> queryByPk(@PathVariable("ruleId") String ruleId) {		
		SRuleInfoVO inVo = new SRuleInfoVO();
				inVo.setRuleId(new String(ruleId));
		SRuleInfoVO outputVo = sRuleInfoService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param sRuleInfo
	 * @return
	 */
	@RequestMapping(value="/s/rule/info", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SRuleInfoVO inSRuleInfoVo) {
		try {
			int outputVo = sRuleInfoService.deleteByPk(inSRuleInfoVo);
			return getResponseData(outputVo);
		} catch (Exception e) {
			return getResponseData(-1);
		}

	}
	
	/**
	 * 根据主键更新信息
	 * @param inSRuleInfoVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/info", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SRuleInfoVO inSRuleInfoVo) {
		UserInfo userInfo = (UserInfo) this.httpRequest.getSession().getAttribute("userInfo");
		inSRuleInfoVo.setUpdateTime(BizUtil.getCurrentTime());
		inSRuleInfoVo.setUpdateUser(userInfo.getUserId());
		try {
			int outputVo = sRuleInfoService.updateByPk(inSRuleInfoVo);
			return getResponseData(outputVo);
		} catch (Exception e) {
			return getResponseData(-1);
		}

	}
	
	/**
	 * 新增数据
	 * @param inSRuleInfoVo
	 * @return
	 */
	@RequestMapping(value="/s/rule/info", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSRuleInfo(@RequestBody SRuleInfoVO inSRuleInfoVo) {
		UserInfo userInfo = (UserInfo) this.httpRequest.getSession().getAttribute("userInfo");
		inSRuleInfoVo.setCreateTime(BizUtil.getCurrentTime());
		inSRuleInfoVo.setCreateUser(userInfo.getUserId());
		inSRuleInfoVo.setUpdateTime(BizUtil.getCurrentTime());
		inSRuleInfoVo.setUpdateUser(userInfo.getUserId());
		try {
			int outputVo = sRuleInfoService.insertSRuleInfo(inSRuleInfoVo);
			return getResponseData(outputVo);
		} catch (Exception e) {
			return getResponseData(-1);
		}
	}

	/**
	 * 查询带规则参数的规则信息
	 * @param infoWithParamVO
	 * @return
	 */
	@RequestMapping(value="/s/rule/info/all", method=RequestMethod.POST)
	public @ResponseBody ResponseData<SRuleInfoWithParamVO> queryRuleInfoWithParam(@RequestBody SRuleInfoWithParamVO infoWithParamVO) {
		SRuleInfoWithParamVO resultVO = sRuleInfoService.queryRuleInfoWithParam(infoWithParamVO);
		return getResponseData(resultVO);
	}

	/**
	 * 更新带规则信息的参数  可新增 也可以更新
	 * @param infoWithParamVO
	 * @return
	 */
	@RequestMapping(value="/s/rule/info/all", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateRuleInfoWithParam(@RequestBody SRuleInfoWithParamVO infoWithParamVO) {
		UserInfo userInfo = (UserInfo) this.httpRequest.getSession().getAttribute("userInfo");
		infoWithParamVO.setUpdateTime(BizUtil.getCurrentTime());
		infoWithParamVO.setUpdateUser(userInfo.getUserId());
		try {
			int num = sRuleInfoService.updateRuleInfoWithParam(infoWithParamVO);
			return getResponseData(num);
		} catch (Exception e) {
			ResponseData<Integer> result = getResponseData(-1);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	/**
	 * 删除带规则信息的参数
	 * @param infoWithParamVO
	 * @return
	 */
	@RequestMapping(value="/s/rule/info/all", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteRuleInfoWithParam(@RequestBody SRuleInfoWithParamVO infoWithParamVO) {
		UserInfo userInfo = (UserInfo) this.httpRequest.getSession().getAttribute("userInfo");
		infoWithParamVO.setUpdateTime(BizUtil.getCurrentTime());
		infoWithParamVO.setUpdateUser(userInfo.getUserId());
		try {
			int num = sRuleInfoService.deleteRuleInfoWithParam(infoWithParamVO);
			return getResponseData(num);
		} catch (Exception e) {
			ResponseData<Integer> rs = getResponseData(-1);
			rs.setMessage(e.getMessage());
			return rs;
		}
	}

}
